package com.action;

import cn.hutool.core.io.FileUtil;
import com.bean.ResponseBean;
import com.config.CopyFile;
import com.pojo.Admin;
import com.pojo.Wuye;
import com.service.SshCopyFileService;
import com.service.WuyeService;
import com.util.PasswordUtil;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: T-bao
 * @Date: 2021-3-26 9:50
 * @Description:
 */
@RestController
@RequestMapping("/wuye")
public class WuyeAction extends CopyFile {

    @Autowired
    private WuyeService wuyeService;
//    @Autowired
//    private AdminRoleService adminRoleService;

    @Value("${upload.dir}")
    private String upload ;

    @Value("${upload.tmp.dir}")
    private String tmp ;

//    @Autowired
//    private SshCopyFileService sshCopyFileService;

    /**
     * @param name  物业公司名称
     * @param page  页数
     * @param limit 每页条数
     * @return  查询物业公司
     */
    @RequestMapping("/selectWuye")
    public ResponseBean selectWuye(String name, Integer page, Integer limit){

////        System.out.println("name="+name+" page="+page+" limit="+limit);
        if (name == "") {
            name = null;
        }
        if(page==1){
            page=0;
        }else{
            page=(page*limit)-limit;
        }
        int count = wuyeService.count(name);
//        System.out.println(wuyeService.selectWuye(name,page,limit));
     //   Page p = new Page(page,limit);
     //   IPage result = wuyeService.selectWuyeByNameWithPage(name,p);
      //  return new ResponseBean(0 , result.getTotal(), "成功", result.getRecords());
        return new ResponseBean(0,count,wuyeService.selectWuye(name,page,limit));
    }

    /**
     * 新增物业公司
     * @param wuye 物业类
     * @return
     */
    @RequestMapping("/addWuye")
    public ResponseBean addWuye(@RequestBody Wuye wuye, HttpServletRequest request) {
//        String logo = wuye.getLogo();
//        if(StringUtil.isNull(logo)){
//            return new ResponseBean(4, 1, "logo不能为空");
//        }
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        wuye.setCreatetime(df.format(new Date()));
        if (null != admin) {
            wuye.setOperUser(admin.getId());
        }

        String password = wuye.getPassword();
//        int m = wuyeService.selectPhone(wuye.getPhone());
        long c = wuyeService.selectWyCode(wuye.getWycode());
//        if(m!=0){
//            return new ResponseBean(3, 1, "联系方式已存在");
//        }else
        if (c != 0) {
            return new ResponseBean(5, 1, "物业编码已存在");
        } else{
            int i = wuyeService.selectWuyeName(wuye.getWyname());
            if (i == 0) {
                wuye.setPassword(PasswordUtil.jiami(password));

//                Admin userinfo = (Admin) request.getSession().getAttribute("admin");
//                List<String> list = adminRoleService.shifouGuanliyuan(userinfo.getId());
//                for (int X = 0; X < list.size(); X++) {
//                    //管理员新增
//                    if (list.get(X) == "管理员" || list.get(X).equals("管理员")) {
                wuye.setAddFlag(1);
                        wuye.setStatus(1);
                        if (wuyeService.save(wuye)) {
                            if(StringUtil.isNotNull(wuye.getLogo())){
                                FileUtil.copy(tmp + wuye.getLogo(), upload, true) ;
//                                sshCopyFileService.copy(upload + wuye.getLogo());
                                //tmp复制图片到upload
//                                String srcPathStr = tmp+wuye.getLogo(); //源文件地址
//                                String desPathStr = upload; //目标文件地址
//                                copy(srcPathStr, desPathStr);
                            }
                            return new ResponseBean(0, 1, "新增成功");
                        } else {
                            return new ResponseBean(1, 1, "新增失败");
                        }
//                    }
//                }
                //录入员新增
//                wuye.setStatus(0);
//                if (wuyeService.save(wuye)) {
//                    if(StringUtil.isNotNull(wuye.getLogo())){
//                        //tmp复制图片到upload
//                        String srcPathStr = tmp+wuye.getLogo(); //源文件地址
//                        String desPathStr = upload; //目标文件地址
//                        copy(srcPathStr, desPathStr);
//                    }
//                    return new ResponseBean(0, 1, "新增成功");
//                } else {
//                    return new ResponseBean(1, 1, "新增失败");
//                }
            } else {
                return new ResponseBean(2, 1, "公司名称已存在");
            }
        }
    }

    /**
     * 删除物业公司
     * @param id  物业公司ID
     * @return
     */
    @RequestMapping("/delwuye")
    public ResponseBean delWuye(String id){

        if(wuyeService.removeById(id)){
            return new ResponseBean(0, 1, "成功", "删除成功");
        }else{
            return new ResponseBean(1, 1, "失败", "删除失败");
        }
    }

    /**
     * 修改物业
     * @param wuye
     * @return
     */
    @RequestMapping("/updatewuye")
    public ResponseBean updateWuye(@RequestBody Wuye wuye){
//        String logo = wuye.getLogo();
//        if(StringUtil.isNull(logo)){
//            return new ResponseBean(4, 1, "logo不能为空");
//        }

        String phone = wuye.getPhone();
        String name = wuye.getWyname();
        String id = wuye.getId();

//        int x = wuyeService.selectPhone(phone);
//        int y = wuyeService.selectPhoneID(phone,id);
//        if (x>=1 && y==0){
//            return new ResponseBean(3, 1, "该联系方式已存在！请重新输入", "");
//        }
        int i = wuyeService.selectWuyeName(name);
        int m = wuyeService.selectWuyeNameID(name,id);
        if(i>=1 && m==0){
            return new ResponseBean(2,1,"重复","物业名称重复");
        }else{
            Wuye wy = wuyeService.selectID(wuye.getId());
            wuye.setStatus(wy.getStatus());
            if(wuyeService.updateById(wuye)){
                if (StringUtil.isNotNull(wuye.getLogo())){
                    if (new File(tmp+wuye.getLogo()).exists())
                    {
                        FileUtil.copy(tmp + wuye.getLogo(), upload, true) ;
                    }


//                    sshCopyFileService.copy(upload + wuye.getLogo());
                    //tmp复制图片到upload
//                    String srcPathStr = tmp+wuye.getLogo(); //源文件地址
//                    String desPathStr = upload; //目标文件地址
//                    copy(srcPathStr, desPathStr);
                }
                return new ResponseBean(0,1,"修改成功","修改成功");
            }else{
                return new ResponseBean(1,1,"修改失败","修改失败");
            }
        }
    }

    /**
     * 查询状态为启用的物业（管理员）
     * @return
     */
    @RequestMapping("/selectwuyestatus")
    public ResponseBean selectWuyeStatus(){
        List<Wuye> wu = wuyeService.Wuyes();
        return new ResponseBean(0,1,"查找启用物业",wu);
    }

    /**
     * 查询状态为启用的物业（制卡员）
     * @return
     */
    @RequestMapping("/selectwuyes")
    public ResponseBean selectWuyes(){
        List<Wuye> wu = wuyeService.WuyeStatus();
        return new ResponseBean(200,1,"查找启用物业",wu);
    }

    /**
     * 修改物业公司状态
     */
    @RequestMapping("/upStatus")
    @ResponseBody
    public String changeStatus(String id) {
        // 修改状态，返回修改后的状态
        Wuye gnaddress = wuyeService.upStatus(id);
        if (gnaddress != null)
            return gnaddress.getStatus() + "";
        return "";
    }

}
